<?php
/**
 * @name GalleryCategoryController
 * @version 2.0
 * @author Nguyen Van Phuong, <phuong.nguyen.itvn@gmail.com>
 * @copyright 2011 PN68 CMS
 */
class GalleryCategoryController extends BaseAdminController {


	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'GalleryCategory'),
		));
	}

	public function actionCreate() {
		$model = new GalleryCategory;


		if (isset($_POST['GalleryCategory'])) {
			$model->attributes = $_POST['GalleryCategory'];

			if ($model->save()) {
			if(isset($_FILES) && $_FILES['image']['error']!=4){
				$last_id = $model->getPrimaryKey();
				switch ($_FILES['image']['type'])
					{
						case 'image/png':
							$ext = 'png';
							break;
						case 'image/gif':
							$ext = 'gif';
							break;
						default:
							$ext = 'jpg';
							break;
					}
				$name = str_replace(".$ext", '', $_FILES['image']['name']);
				$image_name = $last_id.'_cat_'.$name.'.'.$ext;
				$file_upload=CUploadedFile::getInstanceByName("image");
				$dir = dirname(Yii::app()->getBasePath()).'/gallery/full';
				
				if(is_dir($dir)==false) mkdir($dir);
				if($file_upload->saveAs($dir.'/'.$image_name)){
					//resize
					$dir_thumb = dirname(Yii::app()->getBasePath()).'/gallery/thumb';
					$image = Yii::app()->image->load($dir.'/'.$image_name);
					$image->resize(180, 120);
					$image->save($dir_thumb.'/'.$image_name);
					//end resize
					GalleryCategory::model()->updateByPk($last_id, array('image'=>$image_name));
				}
				}
				if (Yii::app()->request->isAjaxRequest)
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'GalleryCategory');


		if (isset($_POST['GalleryCategory'])) {
			$model->attributes = $_POST['GalleryCategory'];
		if(isset($_FILES) && $_FILES['image']['error']==0){
				switch ($_FILES['image']['type'])
					{
						case 'image/png':
							$ext = 'png';
							break;
						case 'image/gif':
							$ext = 'gif';
							break;
						default:
							$ext = 'jpg';
							break;
					}
				$name = str_replace(".$ext", '', $_FILES['image']['name']);
				$image_name = $model->id.'_cat_'.$name.'.'.$ext;
				$file_upload=CUploadedFile::getInstanceByName("image");
				$dir = dirname(Yii::app()->getBasePath()).'/gallery/full';
				if(is_dir($dir)==false) @mkdir($dir);
				if($file_upload->saveAs($dir.'/'.$image_name)){
					//resize
					$dir_thumb = dirname(Yii::app()->getBasePath()).'/gallery/thumb';
					$image = Yii::app()->image->load($dir.'/'.$image_name);
					$image->resize(180, 120);
					$image->save($dir_thumb.'/'.$image_name);
					//end resize
					$model->image = $image_name;
				}
				}
			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->request->isPostRequest) {
			$this->loadModel($id, 'GalleryCategory')->delete();

			if (!Yii::app()->request->isAjaxRequest)
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('GalleryCategory');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new GalleryCategory('search');
		$model->unsetAttributes();

		if (isset($_GET['GalleryCategory']))
			$model->attributes = $_GET['GalleryCategory'];

		$this->render('admin', array(
			'model' => $model,
		));
	}

}